package cn.tsmchina.one.interceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.tsmchina.one.domain.user.User;
import cn.tsmchina.one.service.user.UserService;
import cn.tsmchina.one.util.C;

public class LoginInterceptor implements HandlerInterceptor {
	@Autowired
	private UserService userService;
	public boolean preHandle(HttpServletRequest request,HttpServletResponse response,
		Object handler) throws Exception {
 		String path = request.getServletPath();
		
		//用户注册、登录：要放行
		if(path.startsWith("/user/login")||path.startsWith("/user/register")){
			return true;
		}//doyle否则无法进行前台登录
		if(path.startsWith("/user/register")){
			return true;
		}
		//已经登录，要放行
		if(request.getSession().getAttribute("user")!=null)
			return true;
		else if(request.getSession().getAttribute("user")==null)
		{
			Cookie[] cookies = request.getCookies();  
	        String[] cooks = null;  
	        long cid = 0;  
	        String password = null;
	        String phone = null;
	        String id = null;
	        if (cookies != null) {  
	            for (Cookie coo : cookies) {  
	                String aa = coo.getValue();  
	                //存储时候个数如下
					//Cookie cookie = new Cookie("user",phone+"==="+password+"==="+cid+"==="+u.getId());  
	                cooks = aa.split("===");  
	                if (cooks.length == 4) {  
	                    cid = Long.parseLong(cooks[2].toString());  
	                    password = cooks[1];  
	                    phone = cooks[0];
	                    id = cooks[3];
		                User u = userService.loginValid(phone,password, cid);
		    			if(null != u){
		    				request.getSession().setAttribute("user", u);
		    				request.getSession().setAttribute("userId", u.getId());
		    				
		    				String realName = u.getRealName();
		    				if(StringUtils.isEmpty(realName)||"null".equals(realName)){
		    					realName = "";
		    				}
		    				request.getSession().setAttribute("realName",realName);
		    				request.getSession().setAttribute("phone", u.getPhone());
		    				request.getSession().setAttribute("address", u.getAddress());
		    				return true;
		    			}
	                }
	            }
	            response.sendRedirect(request.getContextPath()+"/user/login");
	            return false;
	        }  
			response.sendRedirect(request.getContextPath()+"/user/login");
			return false;
		}
		return true;
	}
	
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
			Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	public void afterCompletion(HttpServletRequest arg0,
			HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}